<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Common_model');
		$this->load->helper('bantuan_helper');
	}

	function index()
			{		$data['list_event']=$this->Common_model->getData('list','tk_event','id_event',FALSE,5,3,'DESC');
					$data['update_news'] =$this->Common_model->getData('list','tk_content','id_content',FALSE,5,FALSE,'DESC');
					$data['query'] =$this->Common_model->getData('list','tk_event','id_event',FALSE,3,FALSE,'DESC');
										
					$data['main_view'] = 'home';
					$this->load->view('index',$data);
			}
			
			
	function event()
			{		
					$offset = $this->uri->segment(3);
					$config['base_url'] = site_url().'/home/event';
					$config['total_rows'] = count($this->Common_model->getData('list','tk_event','id_event',FALSE,FALSE,FALSE,'DESC'));
					$config['per_page'] = 8;
					$config['uri_segment'] = '3';
					$data['urut'] = $this->uri->segment(3);
					$data['query'] =$this->Common_model->getData('list','tk_event','id_event',FALSE,$config['per_page'],$offset,'DESC');
										
					$data['main_view'] = 'event';
					$this->pagination->initialize($config);
					$this->load->view('index',$data);
					
			}	
			
	function detail()
			{
					$this->form_validation->set_rules('class','Pilih Kelas','required');
					if($this->form_validation->run() == FALSE)
						{
							$id = $this->uri->segment(3);
							$data['row']=$this->Common_model->getData('by_id','tk_event','id_event',$id,FALSE,FALSE,FALSE);
								//query untuk menampilkan data
								$query= $this->db->query("select cl.id_class, cl.id_event, cl.class_name, cl.cost from tk_class cl 
									join tk_event evn on evn.id_event=cl.id_event 
									where cl.id_event=".$id);
									
							
							$data['id_event'] = $this->uri->segment(3);
							$data['query'] = $query->result_array();						
							$data['main_view'] = 'detail_event';
							$this->load->view('index',$data);
						}
					else
						{
						
								$id = $this->uri->segment(3);
								$id_class = $this->input->post('class');
								$session=$this->session->userdata('session_id');
								$qr= $this->db->query("select cl.id_class,cl.percent_ammount, cl.id_event, cl.class_name, cl.cost
								,cl.percent_disc
									from tk_class cl 
									join tk_event evn on evn.id_event=cl.id_event 
									where cl.id_class=".$id_class);
								$res=$qr->row();
							
								// fungsi perbandingan jika tidak ada potongan harga/diskon
								if  ($res->percent_ammount == '')
										{
										
										
										
																$data = array(
																		'session_id'=> $session,
																		'id_event'=> $id,
																		'ammount'=> $res->percent_ammount,
																		'id_class'=> $this->input->post('class'),
																		'percent_disc'=> $res->percent_disc
																			);
													
												
														$this->Common_model->addData('tk_temp',$data);
														redirect('home/order/'.$id);
										}
								else	
										{
																		
																$data = array(
																		'session_id'=> $session,
																		'id_event'=> $id,
																		'ammount'=>$res->percent_ammount,
																		'id_class'=> $this->input->post('class'),
																		'percent_disc'=> $res->percent_disc
																			);
													
												
														$this->Common_model->addData('tk_temp',$data);
														redirect('home/order/'.$id);
										
										}
								
								
						}	
						
			
			
			}
			
	function order()
			{	
				$this->form_validation->set_rules('jml_tiket','Jumlah Tiket','required');
			
				if($this->form_validation->run() == FALSE)
					{
					$session=$this->session->userdata('session_id');
					$id=$this->uri->segment(3);
					// query untuk menampilkan data pada order dari tabel tk_temp
					$query=$this->db->query("select tk_class.class_name,
					tk_temp.session_id,tk_temp.id_event,tk_class.cost,tk_event.nm_event
					,tk_temp.ammount,tk_event.image_url_thumb,tk_temp.percent_disc
					from tk_temp 
					join tk_event on tk_event.id_event=tk_temp.id_event
					join tk_class on tk_class.id_class=tk_temp.id_class
					where tk_temp.session_id='$session' and tk_temp.id_event=".$id);
						
						$data['query']=$query->result_array();
						$data['row'] = $query->row();
					// query untuk total biaya
					$qr_total=$this->db->query("select SUM(ammount) as total from tk_temp
												where session_id='$session' and id_event=".$id);
						$data['rows']=$qr_total->result_array();
						
						
						$data['main_view'] = 'order';
						$this->load->view('index',$data);
					}
				else
					{
						if ($this->input->post('jml_tiket') == '1')
							{
								echo "satu";
							}
						else
							{
									echo "lebih";
							}
					
					}
			}
			
			//fungsi cekout
	function checkout()
			{
			//validasi
			$this->form_validation->set_rules('user_login','Nama','required');
			if ($this->form_validation->run() == FALSE){
			// masih kurang session
				$session=$this->session->userdata('session_id');
					$id=$this->uri->segment(3);
					// query untuk menampilkan data pada order dari tabel tk_temp
					$query=$this->db->query("select tk_class.class_name,
					tk_temp.session_id,tk_temp.id_event,tk_class.cost,tk_event.nm_event
					,tk_temp.ammount,tk_event.image_url_thumb,tk_temp.percent_disc
					from tk_temp 
					join tk_event on tk_event.id_event=tk_temp.id_event
					join tk_class on tk_class.id_class=tk_temp.id_class
					where tk_temp.session_id='$session' and tk_temp.id_event=".$id);
						
						$data['query']=$query->result_array();
						$data['row'] = $query->row();	
					// query untuk total biaya
					$qr_total=$this->db->query("select SUM(ammount) as total from tk_temp
												where session_id='$session' and id_event=".$id);
						$data['rows']=$qr_total->result_array();

				$data['main_view'] = 'checkout';
				$this->load->view('index',$data);
			}
			
		else
			{
			//input data member ke tabel tk_member
				$data= array (
						'nm_member' => $this->input->post('user_login'),
						'date_of_birth' => $this->input->post('date_of_birth'),
						'sex' => $this->input->post('sex'),
						'email' => $this->input->post('email'),
						'password' => MD5($this->input->post('password')),
						'address' => $this->input->post('alamat'),
						'no_hp' => $this->input->post('no_hp'),
						'id_province' => $this->input->post('id_province'),
 						'id_kab' => $this->input->post('id_kab'),
						'post_code' => $this->input->post('kodepos')
				
				
				);
				$this->Common_model->addData('tk_member',$data);
			$id=$this->uri->segment(3);		
			//fungsi antrian 
//fungsi antrian nomer tiket					
				function antrian($quota='',$no_antri){
						$str_quota = strlen($quota);
						$str_antri = strlen($no_antri);
						$jarak = $str_quota - $str_antri;
						
						for($i=0;$i<$jarak;$i++){
						$no_antri = '0'.$no_antri;
						}
						return $no_antri; 
						}
						
						$antrian = antrian('1000','1');	
			// query membaca tk_member untuk mencari id member
				$qr = $this->db->query("select MAX(id_member) as id_member from tk_member");
				$row= $qr->row();			
			$id_member = $row->id_member;				
			$order_code= ($id.'-'.$id.'-'.$antrian);
			$order_date= date("Y-m-d H:i:s");
			//input data ke tk_order_event
				$data= array (
						'order_code' => $order_code, 
						'order_date' => $order_date,
						//'total_ammount' => $ammount,
						'id_member' => $id_member,
						'id_bank' => $this->input->post('bank_id'),
						'marketing_code' => $this->input->post('kode_marketing'),
						//'status' => $status
				
				);
				$this->Common_model->addData('tk_ticket_order',$data);	
				redirect('home');		
			}	
	/*
<?php
function antrian($quota='',$no_antri){
$str_quota = strlen($quota);
$str_antri = strlen($no_antri);
$jarak = $str_quota - $str_antri;

for($i=0;$i<$jarak;$i++){
$no_antri = '0'.$no_antri;
}
return $no_antri; 
}

echo antrian('100','9');

?>
*/

	
			
			}
			
	function login()
			{
			
			}
	// fungsi untuk menampilkan member berhasil mendaftar pada suatu event 
	function success()
			{
			
				$data['main_view'] = 'success';
				$this->load->view('index',$data);
			}		
			
	function ajax_showKab()
			{
				$provId=$this->uri->segment(3);
				$this->db->where('id_province',$provId);
				$data['kab']= $this->db->get('tk_kab')->result_array();
				$this->load->view('/help_kab',$data);
			}
}


/* End of file home.php */
/* Location: ./application/controllers/home.php */